package cn.beatuty.makeup.module.system.controller.admin.msg;

import cn.beatuty.makeup.module.system.controller.admin.msg.vo.MsgPageReqVO;
import cn.beatuty.makeup.module.system.controller.admin.msg.vo.MsgRespDetailVO;
import cn.beatuty.makeup.module.system.controller.admin.msg.vo.MsgRespVO;
import cn.beatuty.makeup.module.system.controller.admin.msg.vo.MsgSaveReqVO;
import cn.beatuty.makeup.module.system.dal.dataobject.msg.MsgDO;
import cn.beatuty.makeup.module.system.service.msg.MsgService;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.math.BigInteger;
import java.util.*;
import java.io.IOException;

import cn.beatuty.makeup.framework.common.pojo.PageParam;
import cn.beatuty.makeup.framework.common.pojo.PageResult;
import cn.beatuty.makeup.framework.common.pojo.CommonResult;
import cn.beatuty.makeup.framework.common.util.object.BeanUtils;
import static cn.beatuty.makeup.framework.common.pojo.CommonResult.success;

import cn.beatuty.makeup.framework.excel.core.util.ExcelUtils;

import cn.beatuty.makeup.framework.operatelog.core.annotations.OperateLog;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import static cn.beatuty.makeup.framework.operatelog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 评论")
@RestController
@RequestMapping("/app/msg")
@Validated
public class MsgController {

    @Resource
    private MsgService msgService;

    @PostMapping("/create")
    @Operation(summary = "创建评论")
    public CommonResult<Long> createMsg(@Valid @RequestBody MsgSaveReqVO createReqVO) {
        return success(msgService.createMsg(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新评论")
    public CommonResult<Boolean> updateMsg(@Valid @RequestBody MsgSaveReqVO updateReqVO) {
        msgService.updateMsg(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除评论")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteMsg(@RequestParam("id") Long id) {
        msgService.deleteMsg(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MsgRespVO> getMsg(@RequestParam("id") Long id) {
        MsgDO msg = msgService.getMsg(id);
        return success(BeanUtils.toBean(msg, MsgRespVO.class));
    }

    @GetMapping("/get/byArticelId")
    @Operation(summary = "获得评论")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<List<MsgRespDetailVO>> getMsgByArticelId(@RequestParam("id") Long id) {
        List<MsgRespDetailVO> msgByArticelId = msgService.getMsgByArticelId(id);
        return success(msgByArticelId);
    }

    @PostMapping("/page")
    @Operation(summary = "获得评论分页")
    public CommonResult<PageResult<MsgRespVO>> getMsgPage(@Valid @RequestBody MsgPageReqVO pageReqVO) {
        PageResult<MsgDO> pageResult = msgService.getMsgPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MsgRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出评论 Excel")
    @PreAuthorize("@ss.hasPermission('app:msg:export')")
    @OperateLog(type = EXPORT)
    public void exportMsgExcel(@Valid MsgPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MsgDO> list = msgService.getMsgPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "评论.xls", "数据", MsgRespVO.class,
                        BeanUtils.toBean(list, MsgRespVO.class));
    }


        public static void main(String[] args) {
            // 定义 64 位数的最大值：2^64 - 1
            BigInteger maxLimit = new BigInteger("18446744073709551615");

            // 定义最小值：0
            BigInteger minLimit = new BigInteger("0");

            // 计算范围
            BigInteger range = maxLimit.subtract(minLimit);

            // 生成随机数
            Random random = new Random();
            BigInteger random64BitNumber;
            do {
                random64BitNumber = new BigInteger(range.bitLength(), random);
            } while (random64BitNumber.compareTo(range) >= 0); // 确保生成的数在范围内

            // 输出 64 位十进制数
            System.out.println("生成的 64 位十进制数: " + random64BitNumber);
        }

}